<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="industryInfoOperationVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="行业代码"
                       v-bind="formItemLayout">
            <a-input v-decorator="['industryType',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>

          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="行业名称"
                       v-bind="formItemLayout">
            <a-input v-decorator="['industryName',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="行业描述"
                       v-bind="formItemLayout">
            <a-input v-decorator="['industryDesc',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="商户经营类型"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="this.isUpd"
              v-decorator="['merBusiType',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="00">实体</a-select-option>
              <a-select-option key="01">虚拟</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="商户组别"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              @focus="selectGrpNo"
              :disabled="this.isUpd"
              v-decorator="['grpNo',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option v-for="(d,index) in grpNoData" :key="d.grpNo">
                {{ d.grpNo }}-{{ d.grpDesc }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button" v-show="this.title !== '查看商户行业类别'">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'IndustryInfoOperation',
    props: {
      industryInfoOperationVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增商户行业类别',
        isStop: false,
        isCancel: false,
        isUpd: false,
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        grpNoData: [],
        info: {},

      }
    },
    computed: {
      show: {
        get: function () {
          return this.industryInfoOperationVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      selectGrpNo(val) {
        this.grpNoData = []

        let param = {}
        if (val) {
          param = {
            q: encodeURIComponent(val),
            pageSize: 20
          }
        } else {
          param = {
            q: '',
            pageSize: 20
          }
        }
        this.$get('admin/mcmp/bMchntBaseInfo/unionMerGrpSelect2List', param).then((r) => {
          let result = r.data.rows
          result.forEach(r => {
            this.grpNoData.push(r);
          })
        })

      },

      reset() {
        this.loading = false
        this.isCancel = false
        this.isUpd = false
        this.isStop = false
        this.title = '新增商户行业类别'
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId

            if (this.title == '新增商户行业类别') {
              this.info.optFlag = 'add'
            } else if (this.title == '修改商户行业类别') {
              this.info.optFlag = 'upd'
            } else if (this.title == '注销商户行业类别') {
              this.info.optFlag = 'cancel'
            }

            if (this.title == '新增商户行业类别') {
              this.$post('/admin/merParam/industryInfo', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            } else if (this.title == '修改商户行业类别') {
              this.$put('/admin/merParam/industryInfo/update', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            } else {
              this.$put('/admin/merParam/industryInfo/cancel', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            }

          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['id', 'industryType', 'industryName', 'industryDesc', 'merBusiType', 'grpNo'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setIndustryInfoFormValues({...info}) {
        let fields = ['id', 'industryType', 'industryName', 'industryDesc', 'merBusiType', 'grpNo']
        if (info.operation == 'cancel') {
          this.isCancel = true
          this.isUpd = true
          this.title = '注销商户行业类别'
        } else if (info.operation == 'view') {
          this.isUpd = true
          this.title = '查看商户行业类别'
        } else {
          this.title = '修改商户行业类别'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>




